<html>
    <head>
        <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="chrome=1">
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no, width=device-width">
   
    <style type="text/css">
      body,html,#container{
        height: 100%;
        margin: 0px
      }
    </style>
    <title>自定义栅格图层</title>
        <script src="js/coordtransform/index.js"></script>

    </head>
    <body>
     <div id="container" tabindex="0"></div>
   <script type="text/javascript" src="https://webapi.amap.com/maps?v=1.4.2&key=8ca468d3fea477076bf82f71df98d010"></script>
    
    <script>
    
   var titleSize=256;
   var initialResolution=156543.03392804062;//2*Math.PI*6378137/titleSize;//
   var originShift=20037508.342789244;//2*Math.PI*6378137/2.0;// 


    /**
 * 根据像素、等级算出坐标
 * @param p
 * @param zoom
 * @return
 */
function  Pixels2Meters(p,zoom){
    return p*Resolution(zoom)-originShift;
}
/**
 * 根据瓦片的x/y等级返回瓦片范围
 * @param tx
 * @param ty
 * @param zoom
 * @return
 */
function TitleBounds(tx,ty,zoom){
    var minX=Pixels2Meters(tx*titleSize,zoom);
    var maxY=-Pixels2Meters(ty*titleSize,zoom);
    var maxX=Pixels2Meters((tx+1)*titleSize,zoom);
    var minY=-Pixels2Meters((ty+1)*titleSize,zoom);

    //转换成经纬度
    minX=Meters2Lon(minX);
    minY=Meters2Lat(minY);
    maxX=Meters2Lon(maxX);
    maxY=Meters2Lat(maxY);
    //经纬度转换米
    //        minX=Lon2Meter(minX);
    //        minY=Lat2Meter(minY);
    //        maxX=Lon2Meter(maxX);
    //        maxY=Lat2Meter(maxY);
    
    var wgc84min = coordtransform.gcj02towgs84(minX,minY);
    var wgc84max = coordtransform.gcj02towgs84(maxX,maxY);

/*
    CoordinateConverter converter  = new CoordinateConverter();
    converter.from(CoordType.GPS);
    converter.coord(new LatLng( minY,minX));
    LatLng min=converter.convert();
    converter.coord(new LatLng(maxY,maxX));
    LatLng max=converter.convert();*/


   /* minX=Lon2Meter(-wgc84min[0]+2*minX);
    minY=Lat2Meter(-wgc84min[1]+2*minY);
    maxX=Lon2Meter(-wgc84max[0]+2*maxX);
    maxY=Lat2Meter(-wgc84max[1]+2*maxY);*/
    return wgc84min[0]+","+wgc84min[1]+","+wgc84max[0]+","+wgc84max[1]+"&WIDTH=256&HEIGHT=256";
}

/**
 * 计算分辨率
 * @param zoom
 * @return
 */
function Resolution(zoom){
    return initialResolution/(Math.pow(2, zoom));
}

/**
 * X米转经纬度
 */
function Meters2Lon(mx){
    var lon=(mx/originShift)*180.0;
    return lon;
}
/**
 * Y米转经纬度
 */
function Meters2Lat(my){
    var lat=(my/originShift)*180.0;
    lat=180.0/Math.PI*(2*Math.atan(Math.exp(lat*Math.PI/180.0))-Math.PI/2.0);
    return lat;
}
/**
 * X经纬度转米
 */
function Lon2Meter(lon){
    var mx= lon*originShift/180.0;
    return mx;
}
/**
 * Y经纬度转米
 */
function Lat2Meter(lat){
    var my=Math.log(Math.tan((90+lat)*Math.PI/360.0))/(Math.PI/180.0
            );
    my=my*originShift/180.0;
    return my;
}


    var googleLayer = new AMap.TileLayer({
           getTileUrl: function(x,y,z){
              //return 'http://120.78.165.155:8090/geoserver/uavgeo_space/wms?vice=WMS&version=1.1.0&request=GetMap&layers=uavgeo_space:common_airport&styles=&srs=EPSG:4326&format=image%2Fpng&TRANSPARENT=true&bbox='+TitleBounds(x,y,z);
              return 'http://120.78.165.155:8090/geoserver/uavgeo_space/wms?vice=WMS&version=1.1.0&request=GetMap&layers=uavgeo_space:airport_protect_area&styles=&srs=EPSG:4326&format=image%2Fpng&TRANSPARENT=true&bbox='+TitleBounds(x,y,z);
           },
           zIndex: 13
    });
    var map = new AMap.Map('container',{
            resizeEnable: true,
            zoom: 13,
            center: [116.39,39.9]
    });
    googleLayer.setMap(map)


</script>

    </body>
</html>

